//
//  BZNavigationController.swift
//  weibo
//
//  Created by zzx on 2017/7/22.
//  Copyright © 2017年 zzx. All rights reserved.
//

import UIKit

class BZNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.interactivePopGestureRecognizer?.delegate = self
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //监听push手势
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0 {
            
            viewController.hidesBottomBarWhenPushed = true
            
        }
        
        
        super.pushViewController(viewController, animated: true)
        
        if viewControllers.count > 1 {
            
            var title: String = ""
            
            if viewControllers.count == 2 {
                
                title = viewControllers.first!.title!
                
                
            }else{
            
                title = "返回"
            
            }
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            
            viewController.navigationItem.title = "当前显示第\(viewControllers.count)个控制器"
            
        }
        
    }
    
    func backAction() {
        
        self.popViewController(animated: true)
        
    }
    
    //MARK:- 设置pop手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if viewControllers.count == 1 {
            
            return false
            
        }else{
            
            return true
        
        }
        
        
    }
    
    
}








